<template>
  <el-dialog
    :title="!dataForm.oid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="客户标识符" prop="cid">
      <el-input v-model="dataForm.cid" placeholder="客户标识符"></el-input>
    </el-form-item>
    <el-form-item label="客户名" prop="cname">
      <el-input v-model="dataForm.cname" placeholder="客户名"></el-input>
    </el-form-item>
    <el-form-item label="客户电话" prop="cphone">
      <el-input v-model="dataForm.cphone" placeholder="客户电话"></el-input>
    </el-form-item>
    <el-form-item label="收货地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="收货地址"></el-input>
    </el-form-item>
    <el-form-item label="商品唯一标识符" prop="pid">
      <el-input v-model="dataForm.pid" placeholder="商品唯一标识符"></el-input>
    </el-form-item>
    <el-form-item label="商品名" prop="pname">
      <el-input v-model="dataForm.pname" placeholder="商品名"></el-input>
    </el-form-item>
    <el-form-item label="商品价格" prop="price">
      <el-input v-model="dataForm.price" placeholder="商品价格"></el-input>
    </el-form-item>
    <el-form-item label="商品数量（数组）" prop="pnum">
      <el-input v-model="dataForm.pnum" placeholder="商品数量（数组）"></el-input>
    </el-form-item>
    <el-form-item label="商品类别唯一标识符" prop="tid">
      <el-input v-model="dataForm.tid" placeholder="商品类别唯一标识符"></el-input>
    </el-form-item>
    <el-form-item label="商品一级分类名" prop="ftname">
      <el-input v-model="dataForm.ftname" placeholder="商品一级分类名"></el-input>
    </el-form-item>
    <el-form-item label="商品二级分类名" prop="stname">
      <el-input v-model="dataForm.stname" placeholder="商品二级分类名"></el-input>
    </el-form-item>
    <el-form-item label="下单时间" prop="orderDate">
      <el-input v-model="dataForm.orderDate" placeholder="下单时间"></el-input>
    </el-form-item>
    <el-form-item label="审批时间" prop="examDate">
      <el-input v-model="dataForm.examDate" placeholder="审批时间"></el-input>
    </el-form-item>
    <el-form-item label="出库时间" prop="startDate">
      <el-input v-model="dataForm.startDate" placeholder="出库时间"></el-input>
    </el-form-item>
    <el-form-item label="到达分站时间" prop="transDate">
      <el-input v-model="dataForm.transDate" placeholder="到达分站时间"></el-input>
    </el-form-item>
    <el-form-item label="出站时间" prop="outTransDate">
      <el-input v-model="dataForm.outTransDate" placeholder="出站时间"></el-input>
    </el-form-item>
    <el-form-item label="签收时间" prop="finishDate">
      <el-input v-model="dataForm.finishDate" placeholder="签收时间"></el-input>
    </el-form-item>
    <el-form-item label="取消时间" prop="cancelDate">
      <el-input v-model="dataForm.cancelDate" placeholder="取消时间"></el-input>
    </el-form-item>
    <el-form-item label="途径中转站标识符" prop="trid">
      <el-input v-model="dataForm.trid" placeholder="途径中转站标识符"></el-input>
    </el-form-item>
    <el-form-item label="配送员标识符" prop="did">
      <el-input v-model="dataForm.did" placeholder="配送员标识符"></el-input>
    </el-form-item>
    <el-form-item label="配送员名字" prop="dname">
      <el-input v-model="dataForm.dname" placeholder="配送员名字"></el-input>
    </el-form-item>
    <el-form-item label="配送费" prop="deliverPrice">
      <el-input v-model="dataForm.deliverPrice" placeholder="配送费"></el-input>
    </el-form-item>
    <el-form-item label="订单是否结束（0表示未结束，1表示结束）" prop="isend">
      <el-input v-model="dataForm.isend" placeholder="订单是否结束（0表示未结束，1表示结束）"></el-input>
    </el-form-item>
    <el-form-item label="订单状态（状态码:已结束状态/未结束状态）（0签收/挂起;1审批未通过/待审批;2在分站/待发货;3在配送员/待入站;4在仓库/待出站;5确定取消/待签收）" prop="status">
      <el-input v-model="dataForm.status" placeholder="订单状态（状态码:已结束状态/未结束状态）（0签收/挂起;1审批未通过/待审批;2在分站/待发货;3在配送员/待入站;4在仓库/待出站;5确定取消/待签收）"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          oid: 0,
          cid: '',
          cname: '',
          cphone: '',
          address: '',
          pid: '',
          pname: '',
          price: '',
          pnum: '',
          tid: '',
          ftname: '',
          stname: '',
          orderDate: '',
          examDate: '',
          startDate: '',
          transDate: '',
          outTransDate: '',
          finishDate: '',
          cancelDate: '',
          trid: '',
          did: '',
          dname: '',
          deliverPrice: '',
          isend: '',
          status: ''
        },
        dataRule: {
          cid: [
            { required: true, message: '客户标识符不能为空', trigger: 'blur' }
          ],
          cname: [
            { required: true, message: '客户名不能为空', trigger: 'blur' }
          ],
          cphone: [
            { required: true, message: '客户电话不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '收货地址不能为空', trigger: 'blur' }
          ],
          pid: [
            { required: true, message: '商品唯一标识符不能为空', trigger: 'blur' }
          ],
          pname: [
            { required: true, message: '商品名不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '商品价格不能为空', trigger: 'blur' }
          ],
          pnum: [
            { required: true, message: '商品数量（数组）不能为空', trigger: 'blur' }
          ],
          tid: [
            { required: true, message: '商品类别唯一标识符不能为空', trigger: 'blur' }
          ],
          ftname: [
            { required: true, message: '商品一级分类名不能为空', trigger: 'blur' }
          ],
          stname: [
            { required: true, message: '商品二级分类名不能为空', trigger: 'blur' }
          ],
          orderDate: [
            { required: true, message: '下单时间不能为空', trigger: 'blur' }
          ],
          examDate: [
            { required: true, message: '审批时间不能为空', trigger: 'blur' }
          ],
          startDate: [
            { required: true, message: '出库时间不能为空', trigger: 'blur' }
          ],
          transDate: [
            { required: true, message: '到达分站时间不能为空', trigger: 'blur' }
          ],
          outTransDate: [
            { required: true, message: '出站时间不能为空', trigger: 'blur' }
          ],
          finishDate: [
            { required: true, message: '签收时间不能为空', trigger: 'blur' }
          ],
          cancelDate: [
            { required: true, message: '取消时间不能为空', trigger: 'blur' }
          ],
          trid: [
            { required: true, message: '途径中转站标识符不能为空', trigger: 'blur' }
          ],
          did: [
            { required: true, message: '配送员标识符不能为空', trigger: 'blur' }
          ],
          dname: [
            { required: true, message: '配送员名字不能为空', trigger: 'blur' }
          ],
          deliverPrice: [
            { required: true, message: '配送费不能为空', trigger: 'blur' }
          ],
          isend: [
            { required: true, message: '订单是否结束（0表示未结束，1表示结束）不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '订单状态（状态码:已结束状态/未结束状态）（0签收/挂起;1审批未通过/待审批;2在分站/待发货;3在配送员/待入站;4在仓库/待出站;5确定取消/待签收）不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.oid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.oid) {
            this.$http({
              url: this.$http.adornUrl(`//order/info/${this.dataForm.oid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.cid = data.order.cid
                this.dataForm.cname = data.order.cname
                this.dataForm.cphone = data.order.cphone
                this.dataForm.address = data.order.address
                this.dataForm.pid = data.order.pid
                this.dataForm.pname = data.order.pname
                this.dataForm.price = data.order.price
                this.dataForm.pnum = data.order.pnum
                this.dataForm.tid = data.order.tid
                this.dataForm.ftname = data.order.ftname
                this.dataForm.stname = data.order.stname
                this.dataForm.orderDate = data.order.orderDate
                this.dataForm.examDate = data.order.examDate
                this.dataForm.startDate = data.order.startDate
                this.dataForm.transDate = data.order.transDate
                this.dataForm.outTransDate = data.order.outTransDate
                this.dataForm.finishDate = data.order.finishDate
                this.dataForm.cancelDate = data.order.cancelDate
                this.dataForm.trid = data.order.trid
                this.dataForm.did = data.order.did
                this.dataForm.dname = data.order.dname
                this.dataForm.deliverPrice = data.order.deliverPrice
                this.dataForm.isend = data.order.isend
                this.dataForm.status = data.order.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`//order/${!this.dataForm.oid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'oid': this.dataForm.oid || undefined,
                'cid': this.dataForm.cid,
                'cname': this.dataForm.cname,
                'cphone': this.dataForm.cphone,
                'address': this.dataForm.address,
                'pid': this.dataForm.pid,
                'pname': this.dataForm.pname,
                'price': this.dataForm.price,
                'pnum': this.dataForm.pnum,
                'tid': this.dataForm.tid,
                'ftname': this.dataForm.ftname,
                'stname': this.dataForm.stname,
                'orderDate': this.dataForm.orderDate,
                'examDate': this.dataForm.examDate,
                'startDate': this.dataForm.startDate,
                'transDate': this.dataForm.transDate,
                'outTransDate': this.dataForm.outTransDate,
                'finishDate': this.dataForm.finishDate,
                'cancelDate': this.dataForm.cancelDate,
                'trid': this.dataForm.trid,
                'did': this.dataForm.did,
                'dname': this.dataForm.dname,
                'deliverPrice': this.dataForm.deliverPrice,
                'isend': this.dataForm.isend,
                'status': this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
